<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>信管帮-邮件接口测试</title>
    <style>
        *{
            margin: 0;
            padding: 0;
        }
        html, body{
            height: 100%;
        }
        body{
            display: flex;
            flex-direction: column;
            font-size: 1.1111rem;
            font-family: 微软雅黑;
        }
        header{
            height: 3.1401rem;
            color: #fff;
            background: #2493f1;
            text-align: center;
            line-height: 3.1401rem;
            box-shadow: .2415rem .2415rem .4831rem #b1b1b1;
        }
        section{
            flex: 1;
            display: flex;
            flex-direction: column;
            align-items: center;
        }
        .container{
            margin-top: 3.6232rem;
            border-radius: .3623rem;
            width: 90%;
            background: #bcf0ff;
            box-sizing: border-box;
            padding: .9662rem;
            display: flex;
            flex-direction: column;
            align-items: center;
        }
        .container>div span{
            margin-right: .3623rem;
        }
        input{
            font-size: 1.1353rem;
            padding-left: .2415rem;
            border-radius: .1208rem;
        }
        #code{
            width: 6.0386rem;
        }
        
        button{
            display: inline-block;
            border: none;
            border-radius: .2415rem;
            font-size: 1.087rem;
            background: #2493f1;
            color: #fff;
            margin-top: 1.2077rem;
            margin-bottom: 1.2077rem;
            margin-left: 50%;
            transform: translateX(-50%);
            width: 7.7295rem;
            height: 1.9324rem;
        }
        .btn1{
            background: #b8b8b8;
        }
        .box2{
            display: none;
        }
    </style>
    <script src="https://unpkg.com/axios/dist/axios.min.js"></script>
</head>
<body>
    <header>邮件接口测试</header>
    <section>
        <div class="container">
            <div>
                <span>请输入邮箱</span><input type="text" name="" id="email" placeholder="*****@qq.com">
                <button disabled class="btn1" type="button" onclick="handleClick(1)">发送验证码</button>
            </div>         
            <div class="box2">
                <span>填写验证码</span><input type="text" name="" id="code" placeholder="">
                <button class="btn2" type="button" onclick="handleClick(2)">验证</button>
            </div>        
            
        </div>
        
    </section>
    <script>
        var email = document.getElementById("email")
        var code = document.getElementById("code")
        var btn1 = document.querySelector(".btn1")
        var btn2 = document.querySelector(".btn2")
        function changeBtn1(r) {
            if(r === 1) {//disabled=true
                btn1.style.background='#b8b8b8'
                btn1.disabled=true
                
            }else if(r === 2){//disabled=false
                btn1.disabled=false
                btn1.style.background='#2493f1'
            }
        }
        email.oninput =()=>{
            var myEmail = email.value
            if(myEmail.substring(myEmail.length-7, myEmail.length) !== '@qq.com') {
                console.warn('邮箱格式不正确')
                changeBtn1(1)
                
            }else{
                console.log('邮箱格式正确')
                changeBtn1(2)
            }
        }


        function handleClick(r) {
            var myEmail = email.value
            switch (r) {
                case 1:
                    if(myEmail.substring(myEmail.length-7, myEmail.length) !== '@qq.com') {
                        changeBtn1(1)
                    }else{
                        changeBtn1(2)
                        console.log("btn1被点击")
                        //发送请求
                        axios.post("https://fc-mp-e8e2bde5-0c0c-4622-87b5-66f8b08c3856.next.bspapp.com/emailCode", {method: 'sendCode', email: email.value})
                        .then(res=>{
                            console.log(res.data)
                            if(res.data.status === 1){//发送成功
                                btn1.innerHTML='已发送'
                                changeBtn1(1)
                                document.querySelector(".box2").style.display='block'
                                var codeId = res.data.id
                                //document.querySelector(".btn2").onclick=handleClick(2,codeId)
                                btn2.setAttribute("codeId", codeId)
                            }else{
                                btn1.style.innerHTMLl='发送失败'
                                changeBtn1(2)
                            }
                        })
                        
                    };
                break;
                case 2:
                var codeId = btn2.getAttribute("codeId")
                console.log('method: '+'validateCode')
                console.log('codeId: '+codeId)
                console.log('email: '+email.value)
                console.log('code: '+code.value)
                    axios.post("https://fc-mp-e8e2bde5-0c0c-4622-87b5-66f8b08c3856.next.bspapp.com/emailCode",{method: 'validateCode', codeId: codeId, email: email.value, code: code.value})
                    .then(res=>{
                        console.log(res.data)
                        switch (res.data.status) {
                            case 1:
                                btn2.innerHTML='验证成功！'
                                break;
                            case 0:
                                btn2.innerHTML='验证失败！'
                                break;
                            
                            case -2:
                                btn2.innerHTML='邮箱无效'
                                break;
                            
                            case -4:
                                btn2.innerHTML='已验证'
                                break;
                            
                            case -3:
                                btn2.innerHTML='验证码过期'
                                break;
                            
                            case -1:
                                btn2.innerHTML='未发送验证码'
                                break;
                            
                        }
                        setTimeout(()=>{
                            btn2.innerHTML='验证'
                        }, 5000)
                    })
                break;
            }
        }
    </script>
</body>
</html>